package com.xqt.base.service;

import com.xqt.base.entity.Operator;
import com.xqt.core.ClassReflectUtil;
import com.xqt.enums.Deleted;
import com.xqt.util.DateUtils;
import com.xqt.util.SessionLocalUtils;

import java.util.UUID;

public abstract class BaseService<T> {

	public void preAdd(T t) throws Exception {
		// 设置主键.字符类型采用UUID,数字类型采用自增
		ClassReflectUtil.setFieldValue(t, "id", UUID.randomUUID().toString());
		// 设置新建人与新建日期
		ClassReflectUtil.setFieldValue(t, "createDate", DateUtils.getDate());
		Operator operator = SessionLocalUtils.getOperator();
		if (operator != null && operator.getMember() != null) {
			ClassReflectUtil.setFieldValue(t, "createBy", operator.getMember()
					.getId());
		}
		// 设置删除标记位为正常
		ClassReflectUtil.setFieldValue(t, "delFlag", Deleted.DELETED_NO.key);

		// 设置更新人与更新日期
		preUpdate(t);
	}

	public void preUpdate(T t) throws Exception {
		// 设置更新人与更新日期
		ClassReflectUtil.setFieldValue(t, "updateDate", DateUtils.getDate());
		Operator operator = SessionLocalUtils.getOperator();
		if (operator != null && operator.getMember() != null) {
			ClassReflectUtil.setFieldValue(t, "updateBy", operator.getMember()
					.getId());
		}
	}
}
